package data.conference;

import java.util.ArrayList;
import java.util.Scanner;

import sql.SQLInterface;

import data.event.Event;

public class Conference
{
	private String name;
	public String name() { return name; }
	public void setName(String name) { this.name = name; }
	
	private String description;
	public String description() { return description; }
	public void setDescription(String description) { this.description = description; }
	
	private ArrayList<String> locations;
	public ArrayList<String> locations() { return locations; }
	
	private ArrayList<Event> events;
	public ArrayList<Event> events() { return events; }
	
	public Conference(String name, String description)
	{
		this.name = name;
		this.description = description;
		locations = new ArrayList<String>();
		events = new ArrayList<Event>();
	}
	
	public Conference(String data)
	{
		setFromData(data);
	}
	
	public void setFromData(String data)
	{
		Scanner dataScanner = new Scanner(data);
		dataScanner.useDelimiter("@");
		name = dataScanner.next();
		description = dataScanner.next();
		locations = stringsFromData(dataScanner.next());
		events = eventsFromData(dataScanner.next());
	}
	
	public String toData()
	{
		return 	name + "@" +
				description + "@" +
				stringsToData(locations) + "@" +
				eventsToData() + "@";
	}
	
	private ArrayList<Event> eventsFromData(String data)
	{
		ArrayList<Event> attendees = new ArrayList<Event>();
		if(!data.equals("_"))
		{
			Scanner dataScanner = new Scanner(data);
			dataScanner.useDelimiter("_");
			while(dataScanner.hasNext())
				attendees.add(new Event(dataScanner.next()));
		}
		return attendees;
	}
	
	private String eventsToData()
	{
		if(events.size() == 0)
			return "_";
		String data = "";
		for(int i = 0; i < events.size(); i++)
		{
			if(i > 0)
				data += "_";
			data += events.get(i).toData();
		}
		return data;
	}
	
	private ArrayList<String> stringsFromData(String data)
	{
		ArrayList<String> strings = new ArrayList<String>();
		if(!data.equals("_"))
		{
			Scanner dataScanner = new Scanner(data);
			dataScanner.useDelimiter("_");
			while(dataScanner.hasNext())
				strings.add(dataScanner.next());
		}
		return strings;
	}
	
	private String stringsToData(ArrayList<String> strings)
	{
		if(strings.size() == 0)
			return "_";
		String data = "";
		for(int i = 0; i < strings.size(); i++)
		{
			if(i > 0)
				data += "_";
			data += strings.get(i);
		}
		return data;
	}
	
	public void saveData() { SQLInterface.saveConference(this); }
}